package com.iris.live.services.report.sources;

import com.iris.live.services.data.repositories.FinanceRepository;
import com.iris.live.services.models.PageData;
import com.iris.live.services.report.Cell;
import com.iris.live.services.report.IExcelReportSource;
import com.iris.live.services.report.Row;
import com.iris.live.services.report.Sheet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by lusong on 2017/5/8.
 */
@Component(value = "ExportFinanceLoanInfoSource")
public class ExportFinanceLoanInfoSource implements IExcelReportSource {
    private Logger logger = LoggerFactory.getLogger(ExportFinanceLoanInfoSource.class);

    @Autowired
    private FinanceRepository financeRepository;

    @Override
    public Sheet[] getSheets(Map<String, Object> context) {
        Sheet[] sheets = new Sheet[1];
        sheets[0] = new Sheet("经销商_金融贷款客户管理导出模板","经销商_金融贷款客户管理导出模板",true);
        return sheets;
    }

    @Override
    public List<Row> getSheetRows(Map<String, Object> context, Sheet sheet) throws ParseException {
        String permit = context.get("permit") == null ? null : context.get("permit").toString();
        String cusName = context.get("cusName") == null ? null : context.get("cusName").toString();
        String cusTel = context.get("cusTel") == null ? null : context.get("cusTel").toString();
        String cusType = context.get("cusType") == null ? null : context.get("cusType").toString();
        String companyName = context.get("companyName") == null ? null : context.get("companyName").toString();
        String state = context.get("state") == null ? null : context.get("state").toString();
        int pageSize = Integer.parseInt(context.get("pageSize") == null ? "0" : context.get("pageSize").toString());
        int pageIndex = Integer.parseInt(context.get("pageIndex") == null ? "0" : context.get("pageIndex").toString());
        String applyStartDate = context.get("applyStartDate") == null ? null : context.get("applyStartDate").toString();  // 申请日期  开始
        String applyEndDate = context.get("applyEndDate") == null ? null : context.get("applyEndDate").toString();  // 申请日期  截止
        String applyPassStartDate = context.get("applyPassStartDate") == null ? null : context.get("applyPassStartDate").toString();  // 申请通过日期 开始
        String applyPassEndDate = context.get("applyPassEndDate") == null ? null : context.get("applyPassEndDate").toString();  // 申请通过日期 截止
        String loanStartDate = context.get("loanStartDate") == null ? null : context.get("loanStartDate").toString();  // 放款到账日期 开始
        String loanEndDate = context.get("loanEndDate") == null ? null : context.get("loanEndDate").toString();   // 放款到账日期 截止

        List<Row> list = new ArrayList<>();
        list.add(new Row());
        list.add(new Row());
        list.add(new Row());
        list.add(new Row());
        list.add(new Row());
        list.add(new Row());

        PageData pageData = financeRepository.exportFinanceLoanInfo(permit, cusName, cusTel, cusType, companyName, state, pageSize, pageIndex,
                applyStartDate,applyEndDate,applyPassStartDate,applyPassEndDate,loanStartDate,loanEndDate);
        if(pageData != null) {
            List resultValue = pageData.getData();
            if (resultValue != null && !resultValue.isEmpty()) {
                for (Object o : resultValue) {
                    Map<String, Object> map = (Map) o;
                    list.add(new Row(getCells(map)));
                }
            }
        }
        return list;
    }

    private Cell[]  getCells(Map<String, Object> map) {
        Cell[] cells = new Cell[18];
        int index = 0;
        cells[index++] = new Cell(map.get("所属金融专员") == null ? "" : map.get("所属金融专员").toString(), index);
        cells[index++] = new Cell(map.get("贷款人类型") == null ? "" : map.get("贷款人类型").toString(), index);
        cells[index++] = new Cell(map.get("贷款人姓名") == null ? "" : map.get("贷款人姓名").toString(), index);
        cells[index++] = new Cell(map.get("贷款人身份证") == null ? "" : map.get("贷款人身份证").toString(), index);
        cells[index++] = new Cell(map.get("贷款人联系电话") == null ? "" : map.get("贷款人联系电话").toString(), index);
        cells[index++] = new Cell(map.get("联系人姓名") == null ? "" : map.get("联系人姓名").toString(), index);
        cells[index++] = new Cell(map.get("联系人身份证") == null ? "" : map.get("联系人身份证").toString(), index);
        cells[index++] = new Cell(map.get("联系人联系电话") == null ? "" : map.get("联系人联系电话").toString(), index);
        cells[index++] = new Cell(map.get("热销级别") == null ? "" : map.get("热销级别").toString(), index);
        cells[index++] = new Cell(map.get("按揭类型") == null ? "" : map.get("按揭类型").toString(), index);
        cells[index++] = new Cell(map.get("金融机构") == null ? "" : map.get("金融机构").toString(), index);
        cells[index++] = new Cell(map.get("审批通过后状态跟踪") == null ? "" : map.get("审批通过后状态跟踪").toString(), index);
        cells[index++] = new Cell(map.get("金融类型") == null ? "" : map.get("金融类型").toString(), index);
        cells[index++] = new Cell(map.get("是否开票") == null ? "" : map.get("是否开票").toString(), index);
        cells[index++] = new Cell(map.get("贷款到期日") == null ? "" : map.get("贷款到期日").toString(), index);
        cells[index++] = new Cell(map.get("申请日期") == null ? "" : map.get("申请日期").toString(), index);
        cells[index++] = new Cell(map.get("通过日期") == null ? "" : map.get("通过日期").toString(), index);
        cells[index++] = new Cell(map.get("放款到账日期") == null ? "" : map.get("放款到账日期").toString(), index);
        return cells;
    }

}
